#include "knl_archive.h"

#define knl_archive_size 256

namespace knl
{
	knl_archive::knl_archive(void)
	{
		m_archive.reset(new knlByte[knl_archive_size]);
		m_Count = knl_archive_size;
		m_outOffset = 0;
		m_inOffset = 0;
	}

	knl_archive::knl_archive( knlByte* _buff,int _len )
	{
		m_archive.reset(new knlByte[_len+1]);
		m_Count = _len;
		m_outOffset = 0;
		m_inOffset = 0;

		write(_buff,_len);
	}


	knl_archive::~knl_archive(void)
	{
	}

	knlByte* knl_archive::getBuffer()
	{
		return m_archive.get();
	}

	void knl_archive::resize( int _newCount )
	{
		if ( _newCount + m_inOffset > m_Count )
		{
			boost::shared_array<knlByte> _temp( new knlByte[m_Count] );
			memcpy(_temp.get(),m_archive.get(),m_Count);
			m_archive.reset( new knlByte[m_Count + _newCount] );
			memcpy(m_archive.get(),_temp.get(),m_Count);
			m_Count += _newCount;
		}
	}

	void knl_archive::write( knlByte* _buff,int _len )
	{
		resize(_len);
		memcpy(m_archive.get() + m_inOffset,_buff,_len);
		m_inOffset = _len;
	}

	void knl_archive::read( knlByte* _buff,int _len )
	{
		memcpy(_buff,m_archive.get() + m_outOffset,_len);
		m_outOffset = _len;
	}
}